import React, { Component } from 'react';
import { Route, Switch, Redirect } from 'react-router-dom';
import { connect } from 'react-redux';
import { bindActionCreators } from 'redux';
import LazyRoute from 'components/lazyRoute';
import * as loginActions from 'actions/login';
import * as chatActions from 'actions/chat';
import socket from 'utils/socket';
import chat from './reducers/chat';


class App extends Component {
    render() {
        return (
            <Switch>               
                <Route path='/login' exact render={props => <LazyRoute {...props} component={import('pages/login')}/>} /> 
                <InnerRoute />
            </Switch>
        );
    }
}

@connect(state => {
    return {
        user: state.login.user,
        isLogin: state.login.isLogin
    }
}, dispatch => ({
    loginActions: bindActionCreators(loginActions, dispatch),
    chatActions: bindActionCreators(chatActions, dispatch)
}))
class InnerRoute extends Component {
    constructor(props) {
        super(props);
        
    }
    async componentDidMount() {
        await this.props.loginActions.getUser();   
        await this.props.chatActions.getLinkmansLastMessage();
        this.props.chatActions.receiveMsg();
    }
    
    render() {
        if (this.props.isLogin === '...') {
            return null;
        } else if (this.props.isLogin === false) {
            return <Redirect to='/login' />
        } else {
            return [
                <Route path='/home' key='home' render={props => <LazyRoute {...props} component={import('pages/home/index')}/>} />,
                <Route path='/addFriend' key='addFriend' render={props => <LazyRoute {...props} component={import('pages/add/index')}/>} />,
                <Route path='/chat/:type/:id' key='chat' render={props => <LazyRoute {...props} component={import('pages/chat/index')}/>} />
                
            ]
        }
        
    }
}



export default App;
